<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                请选择
                <el-select v-model="query.type" placeholder="请选择">
                    <el-option key="t" label="电话号码" value="t"></el-option>
                    <el-option key="x" label="他趣Id" value="x"></el-option>
                    <el-option key="i" label="Id" value="i"></el-option>
                    <el-option key="p" label="直属上级Id" value="p"></el-option>
                </el-select>
                <el-input v-model="query.mobile"  placeholder="左侧下拉框选择搜索方式"  class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="Id"  align="center"></el-table-column>
                <el-table-column prop="pid" label="上级Id"></el-table-column>
                <el-table-column prop="mobile" width="130px" label="手机号码"></el-table-column>
                <el-table-column prop="miyou_id" width="130px" label="他趣Id"></el-table-column>
                <el-table-column prop="nickname" label="微信昵称"></el-table-column>
                <el-table-column prop="create_time" width="180px" label="注册时间"></el-table-column>
                <el-table-column prop="login_time" width="180px"  label="最后登录时间"></el-table-column>
                <el-table-column prop="commission" label="佣金"></el-table-column>
                <el-table-column prop="total_commission" label="总佣金"></el-table-column>
                <el-table-column prop="achievement" label="总业绩"></el-table-column>
                <el-table-column prop="my_achievement" label="个人业绩" sortable></el-table-column>
                <el-table-column prop="display_reward_rate" label="佣金比例" sortable></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                @click="beginEdit(scope.row)"
                        >修改用户</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, next, pager"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>

            <el-dialog
                    title="修改用户信息"
                    :visible.sync="editVisible"
                    width="30%">

                <el-form ref="form" :model="form" label-width="100px">
                    <el-form-item label="他趣Id">
                        <el-input v-model="form.miyou_id"></el-input>
                    </el-form-item>
                    <el-form-item label="佣金比例">
                        <el-input v-model="form.reward_rate"></el-input>为0则计算,设置了就用固定值
                    </el-form-item>
                    <el-form-item label="是否禁用">
                        <el-select v-model="form.state" placeholder="请选择">
                            <el-option v-for="item in selectArr" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否管理员">
                        <el-select v-model="form.admin" placeholder="请选择">
                            <el-option v-for="item in selectArr" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="doEdit()">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>
<script>
export default {
    name: 'order',
    data() {
        return {
            query: {
                type: 't',
                mobile: '',
                page: 1,
                pageSize: 10
            },
            tableData: [],
            pageTotal: 0,
            editVisible: false,
            form: {
                id: 0,
                reward_rate: 0,
                admin: 0,
                state: 0,
                miyou_id: ''
            },
            selectArr:[ {value: 0,label: '否'},{value: 1,label: '是'}]
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.http.user(this.query).then(res => {
                if(res.code === 0){
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.total;
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        doEdit() {
            this.http.userEdit(this.form).then(res => {
                this.getData();
            });
            this.editVisible = false
        },
        beginEdit(user) {
            this.form.admin = user.admin
            this.form.state = user.state
            this.form.miyou_id = user.miyou_id

            this.form.id = user.id
            this.form.reward_rate = user.reward_rate
            this.editVisible = true
        }

    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
